/**
 * Created by jackliu on 15/12/16.
 */

var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var ItbBaseModel = require('./ItbBaseModel');
var async  = require( 'async'); // 同步控制


// Schema 结构
var ItbPrintParamModel = function(obj){
    ItbBaseModel.call(this,obj);

  // 以下为表定义数据，需要的字段就填入
  //`pp_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '印刷参数id',
  //`pp_name` varchar(50) NOT NULL DEFAULT '' COMMENT '套餐名称',
  //`name` varchar(50) NOT NULL COMMENT '内部名称',
  //`desc` varchar(500) NOT NULL DEFAULT '' COMMENT '套餐简介',
  //`size_id` bigint(20) NOT NULL COMMENT '尺寸',
  //`style` varchar(100) NOT NULL COMMENT '样式',
  //`mat` varchar(100) NOT NULL COMMENT '材质',
  //`tech` varchar(100) NOT NULL DEFAULT '' COMMENT '工艺',
  //`state` int(2) NOT NULL DEFAULT '0' COMMENT '状态：有效无效等',
  //`thumbs` text NOT NULL COMMENT '缩略图数组',
  //`create_time` datetime NOT NULL,
  //`update_time` datetime NOT NULL,
  //`update_by` varchar(50) NOT NULL DEFAULT '',
  //`del_flg` int(1) NOT NULL DEFAULT '0',

};
ItbPrintParamModel.prototype = new ItbBaseModel();

// 取得表名
ItbPrintParamModel.prototype.getTableName = function(){
    return 'itb_print_param';
}
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
ItbPrintParamModel.prototype.getWhereConditionOfKeyEqual = function(){
    return 'pp_id='+this.pp_id;
}

// 获取印刷参数／套餐列表
// 参数: itemId	 商品id
// 参数: pageNum	 请求页
// 参数: pageSize	 请求页大小
// 参数：callback 回调，参数如下：
//          error:错误
//          result:多行数据
ItbPrintParamModel.prototype.findPrintParamByItemId = function(itemId, pageNum, pageSize, callback){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        if (err) {
            console.log('ItbPrintParamModel.findPrintParamByItemId Err:'+err);
        }
        // Use the connection
        var sql = ' select pp.*  '+
            ' from itb_print_param pp,  '+
            ' 	itb_print_rel pr '+
            ' where pp.`del_flg`=0 '+
            ' and pr.`del_flg`=0 '+
            ' and pr.`item_id`='+ itemId+
            ' and pr.`pp_id`=pp.`pp_id` '+
            ' order by pp.name asc'+
            ' limit '+pageNum*pageSize+','+pageSize;

        //var sqlValue = [];
        //var sql = mysql.format(sql, sqlValue);
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();

            console.log();

            callback(error, result);
        });
    });
}

// 获取印刷参数／套餐列表 [暂定为:获取子订单中(第一个)印品商品的套餐]
// 参数: orderId	 商品订单id
// 参数: callback 回调，参数如下：
//          error:错误
//          result:多行数据
ItbPrintParamModel.prototype.findPrintParamByOrderId = function(orderId, callback){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        if (err) {
            console.log('ItbPrintParamModel.findPrintParamByOrderId Err:'+err);
        }
        // Use the connection
        var sql = 'SELECT distinct pp.pp_id, pp.pp_name '+
            ' FROM '+
            ' (select d.item_id '+
            '    from itb_trade_order_detail d, itb_items i, tg_type t '+
            '   where d.order_id='+orderId+' and d.del_flg=0 '+
            '     and i.item_id=d.item_id and i.design_id is not null and i.del_flg=0 '+
            '     and t.type_id=i.type_id and t.top_parent=4 and t.del_flg=0 '+// 印品
            '   limit 1) x, '+
            ' itb_print_rel pr, '+
            ' itb_print_param pp '+
            ' WHERE pr.item_id=x.item_id AND pr.del_flg=0 '+
            ' AND pp.pp_id=pr.pp_id AND pp.del_flg=0 '+
            ' ORDER BY pp.pp_id ';

        //var sqlValue = [];
        //var sql = mysql.format(sql, sqlValue);
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();

            console.log();

            callback(error, result);
        });
    });
}

// 获取印刷参数／套餐(带尺寸)列表
// 参数: itemId	 商品id
// 参数: pageNum	 请求页
// 参数: pageSize	 请求页大小
// 参数：callback 回调，参数如下：
//          error:错误
//          result:多行数据
ItbPrintParamModel.prototype.findPrintParamAndSizeByItemId = function(itemId, pageNum, pageSize, callback){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        if (err) {
            console.log('ItbPrintParamModel.findPrintParamByItemId Err:'+err);
        }
        // Use the connection
        var sql = ' select pp.* ,ts.width as width, ts.height as height, ts.print_w as real_width, ts.print_h as real_height, ts.print_unit as real_unit ,ts.desc as real_desc '+
            ' from itb_print_param pp,  '+
            ' 	itb_print_rel pr, '+
            ' 	tg_size ts '+
            ' where pp.`del_flg`=0 '+
            ' and pr.`del_flg`=0 '+
            ' and pr.`item_id`='+ itemId +
            ' and pr.`pp_id`=pp.`pp_id` '+
            ' and ts.`del_flg`=0 '+
            ' and pp.`size_id`=ts.`size_id` '+
            ' order by pp.name asc '+
            ' limit '+pageNum*pageSize+','+pageSize;

        //var sqlValue = [];
        //var sql = mysql.format(sql, sqlValue);
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();

            console.log();

            callback(error, result);
        });
    });
}


// 获取印刷参数／套餐(带尺寸)列表
// 参数: itemIds	 商品ids 逗号拼接
// 参数: pageNum	 请求页
// 参数: pageSize	 请求页大小
// 参数：callback 回调，参数如下：
//          error:错误
//          result:多行数据
ItbPrintParamModel.prototype.findPrintParamAndSizeByItemIds = function(itemIds, pageNum, pageSize, callback){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        if (err) {
            console.log('ItbPrintParamModel.findPrintParamByItemId Err:'+err);
        }
        // Use the connection
        var sql = ' select pp.* ,ts.width as width, ts.height as height, ts.print_w as real_width, ts.print_h as real_height, ts.print_unit as real_unit ,ts.desc as real_desc '+
            ' from itb_print_param pp,  '+
            ' 	itb_print_rel pr, '+
            ' 	tg_size ts '+
            ' where pp.`del_flg`=0 '+
            ' and pr.`del_flg`=0 '+
            ' and pr.`item_id` in ('+ itemIds + ')' +
            ' and pr.`pp_id`=pp.`pp_id` '+
            ' and ts.`del_flg`=0 '+
            ' and pp.`size_id`=ts.`size_id` '+
            ' order by pp.name asc '+
            ' limit '+pageNum*pageSize+','+pageSize;

        //var sqlValue = [];
        //var sql = mysql.format(sql, sqlValue);
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();

            console.log();

            callback(error, result);
        });
    });
}

// 导出
module.exports = ItbPrintParamModel;